package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.BusinessTypeMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.RegionAddDto;
import com.heima.pojo.dto.RegionDetailSearchDto;
import com.heima.pojo.entity.*;
import com.heima.pojo.vo.PageResponseVo;
import com.heima.pojo.vo.RegionDetailVO;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private BusinessTypeMapper businessTypeMapper;
    @Autowired
    private VendingMachineMapper vendingMachineMapper;


    @Override
    public PageResponseVo<RegionVo> searchRegion(Integer pageSize, Integer pageIndex, String name) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            wrapper.like(Region::getName, name);
        }
        Page<Region> regionPage = this.page(new Page<>(pageSize, pageIndex), wrapper);
        ArrayList<RegionVo> regionVos = new ArrayList<>();
        for (Region region : regionPage.getRecords()) {
            RegionVo regionVo = new RegionVo();
            BeanUtils.copyProperties(region, regionVo);
            LambdaQueryWrapper<Node> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Node::getRegionId, region.getId());
            regionVo.setNodeCount(nodeMapper.selectCount(queryWrapper));
            regionVos.add(regionVo);
        }
        PageResponseVo<RegionVo> responseVo = new PageResponseVo<>();
        responseVo.setTotalCount(regionPage.getTotal());
        responseVo.setPageSize(pageSize);
        responseVo.setPageIndex(pageIndex);
        responseVo.setCurrentPageRecords(regionVos);
        responseVo.setTotalPage(regionPage.getPages());
        return responseVo;
    }

    @Override
    public boolean addRegion(RegionAddDto regionAddDto) {
        Region region = new Region();
        region.setName(regionAddDto.getRegionName());
        region.setRemark(regionAddDto.getRemark());
        return this.save(region);
    }

    @Override
    public boolean updateRegion(Long id, RegionAddDto regionAddDto) {
        Region region = new Region();
        region.setName(regionAddDto.getRegionName());
        region.setRemark(regionAddDto.getRemark());
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Region::getId, id);
        return this.update(region, wrapper);

    }

    //根据regionid查询详情
    @Override
    public PageResponseVo<RegionDetailVO> searchRegionDetailById(RegionDetailSearchDto dto) {
        Region region = this.getById(dto.getRegionId());
        LambdaQueryWrapper<Node> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Node::getRegionId, region.getId());
        Page<Node> nodePage = nodeMapper.selectPage(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);
        List<Node> nodes = nodePage.getRecords();
        List<RegionDetailVO> detailVOList = new ArrayList<>();
        for (Node node : nodes) {
            BusinessType businessType = businessTypeMapper.selectById(node.getBusinessId());
            RegionDetailVO detailVO = new RegionDetailVO();
            BeanUtils.copyProperties(node, detailVO);
            detailVO.setBusinessType(businessType);
            detailVOList.add(detailVO);
        }
        //计算每个点位的设备数量 然后赋值给RegionDetailVO
        for (RegionDetailVO detailVO : detailVOList) {
            LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VendingMachine::getNodeId, detailVO.getId());
            detailVO.setVmCount(vendingMachineMapper.selectCount(queryWrapper));
        }
        PageResponseVo<RegionDetailVO> regionDetailVOPageResponseVo = new PageResponseVo<>();
        regionDetailVOPageResponseVo.setTotalCount(nodePage.getTotal());
        regionDetailVOPageResponseVo.setPageSize(dto.getPageSize());
        regionDetailVOPageResponseVo.setPageIndex(dto.getPageIndex());
        regionDetailVOPageResponseVo.setCurrentPageRecords(detailVOList);
        regionDetailVOPageResponseVo.setTotalPage(nodePage.getPages());
        return regionDetailVOPageResponseVo;

    }

    @Override
    public boolean deleteRegionById(Long Id) {
        return this.removeById(Id);
    }
}
